//
//  TTHomeViewController.m
//  TTWeibo
//
//  Created by 陶宏路 on 16/7/20.
//  Copyright © 2016年 陶路路. All rights reserved.
//

#import "TTHomeViewController.h"
#import "TTVisionView.h"
#import "TTHomeVcModel.h"
#import "TTHomeViewCell.h"
#import "TTWeibo-Swift.h"
@interface TTHomeViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,strong) TTHomeVcModel * homeModel;
@property (nonatomic,strong) UIActivityIndicatorView * refresh;
@property (nonatomic,strong) SHRefreshControl * freshControl;

@end

@implementation TTHomeViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.homeModel = [[TTHomeVcModel alloc]init];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
#pragma mark 设置预估行高  自定义Cell 中设置子控件约束的时候 要用 self.contentView
    self.tableView.estimatedRowHeight = 200;
    self.tableView.tableFooterView = self.refresh;
    [self.tableView addSubview:self.freshControl];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.freshControl addTarget:self action:@selector(didFresh) forControlEvents:UIControlEventValueChanged];
    if (!self.isLogin) {
        
        return;
    }
    
    [self getData];
    
}

#pragma mark 下拉刷新 添加事件  [self.freshControl addTarget:self action:@selector(didFresh)

-(void)didFresh{
    
    [self getData];
}

-(void)getData{
    
    [self.homeModel getHomeData:self.refresh.isAnimating sucess:^(BOOL isSuccess, NSInteger count) {
        
        [self.refresh stopAnimating];
        [self.freshControl endRefreshing];
        if (!isSuccess) {
            
            return ;
        }
        
        [self.tableView reloadData];
        
    }];
}

#pragma mark 下拉刷新

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.homeModel.dataArray.count;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    TTHomeViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"homeCell"];
    
    if (cell == nil) {
        
        cell = [[TTHomeViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"homeCell"];
    }
    
    cell.backgroundColor = RANDOMCOLOR;
    cell.cellModel = self.homeModel.dataArray[indexPath.row];

    return cell;
}


#pragma mark tableView 代理方法 找出显示在屏幕非Cell和indexPath

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.row == self.homeModel.dataArray.count - 1 && !self.refresh.isAnimating) {
        
        [self.refresh startAnimating];
        [self getData];
    }
    
}

#pragma mark 下拉刷新 懒加载

-(UIActivityIndicatorView *)refresh{
    
    if (_refresh == nil) {
        
        _refresh = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
        _refresh.color = [UIColor orangeColor];
    }
    
    return _refresh;
}

-(SHRefreshControl *)freshControl{
    
    if (_freshControl == nil) {
        
        _freshControl = [[SHRefreshControl alloc]init];
    }
    
    return _freshControl;
}
@end
